﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BlueRoseGames.Controls.Text
{
    public class PolarTransform : Transform
    {
        #region DependencyProperties

        public static readonly DependencyProperty AngleProperty = Register("Angle", typeof(double), typeof(PolarTransform), 0d);
        public double Angle
        {
            get { return (double)GetValue(AngleProperty); }
            set { SetValue(AngleProperty, value); }
        }

        public static readonly DependencyProperty AngleStretchProperty = Register("AngleStretch", typeof(double), typeof(PolarTransform), 1d);
        public double AngleStretch
        {
            get { return (double)GetValue(AngleStretchProperty); }
            set { SetValue(AngleStretchProperty, value); }
        }

        public static readonly DependencyProperty CenterXProperty = Register("CenterX", typeof(double), typeof(PolarTransform), 0d);
        public double CenterX
        {
            get { return (double)GetValue(CenterXProperty); }
            set { SetValue(CenterXProperty, value); }
        }

        public static readonly DependencyProperty CenterYProperty = Register("CenterY", typeof(double), typeof(PolarTransform), 0d);
        public double CenterY
        {
            get { return (double)GetValue(CenterYProperty); }
            set { SetValue(CenterYProperty, value); }
        }

        public static readonly DependencyProperty RadiusXProperty = Register("RadiusX", typeof(double), typeof(PolarTransform), 50d);
        public double RadiusX
        {
            get { return (double)GetValue(RadiusXProperty); }
            set { SetValue(RadiusXProperty, value); }
        }

        public static readonly DependencyProperty RadiusYProperty = Register("RadiusY", typeof(double), typeof(PolarTransform), 50d);
        public double RadiusY
        {
            get { return (double)GetValue(RadiusYProperty); }
            set { SetValue(RadiusYProperty, value); }
        }

        public static readonly DependencyProperty RadiusStretchProperty = Register("RadiusStretch", typeof(double), typeof(PolarTransform), 1d);
        public double RadiusStretch
        {
            get { return (double)GetValue(RadiusStretchProperty); }
            set { SetValue(RadiusStretchProperty, value); }
        }

        public static readonly DependencyProperty TextDirectionProperty = Register("TextDirection", typeof(Direction), typeof(PolarTransform), Direction.Clockwise);
        public Direction TextDirection
        {
            get { return (Direction)GetValue(TextDirectionProperty); }
            set { SetValue(TextDirectionProperty, value); }
        }

        #endregion

        public override Point TransformPoint(Point p)
        {
            Point ret = new Point();
            double rx = 0;
            double ry=0;
            if (TextDirection == Direction.Clockwise)
            {
                rx = RadiusX - p.Y * RadiusStretch;
                ry = RadiusY - p.Y * RadiusStretch;
            }
            else
            {
                rx = RadiusX + p.Y * RadiusStretch;
                ry = RadiusY + p.Y * RadiusStretch;
            }
            double theta = (((Angle * Math.PI) / 180d) + ((-p.X * AngleStretch) / 100));
            if (TextDirection == Direction.Clockwise)
            {
                theta = -theta;
            }
            ret.Y = Math.Sin(theta) * (ry) + CenterY;
            ret.X = Math.Cos(theta) * (rx) + CenterX;
            return ret;
        }

        public enum Direction
        {
            Clockwise,
            CounterClockwise
        }
    }
}
